home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / initramfs-tools / scripts / functions < prev    next >
Text File  |  2009-09-22  |  7KB  |  369 lines

  1. # -*- shell-script -*-
  2.  
  3. _log_msg()
  4. {
  5.     if [ "$quiet" = "y" ]; then return; fi
  6.     echo "$@"
  7. }
  8.  
  9. log_success_msg()
  10. {
  11.     _log_msg "Success: $@"
  12. }
  13.  
  14. log_failure_msg()
  15. {
  16.     _log_msg "Failure: $@"
  17. }
  18.  
  19. log_warning_msg()
  20. {
  21.     _log_msg "Warning: $@"
  22. }
  23.  
  24. log_begin_msg()
  25. {
  26.     if [ -x /sbin/usplash_write ]; then
  27.         /sbin/usplash_write "TEXT $@"
  28.     fi
  29.     _log_msg "Begin: $@ ..."
  30. }
  31.  
  32. log_end_msg()
  33. {
  34.     if [ -x /sbin/usplash_write ]; then
  35.         /sbin/usplash_write "SUCCESS ok"
  36.     fi
  37.     _log_msg "Done."
  38. }
  39.  
  40. # Add failure hook
  41. add_mountroot_fail_hook()
  42. {
  43.     mkdir -p /tmp/mountroot-fail-hooks.d
  44.     ln -s "$0" /tmp/mountroot-fail-hooks.d/"$1"
  45. }
  46.  
  47. # Run failure hooks.
  48. # When a failure hook exits "1", it has not done anything to correct the
  49. # system.  Exiting "0" means that something has been attempted to resolve
  50. # the lack of a root filesystem.
  51. # Hooks are run in lexigraphical order, and are responsible for removing
  52. # themselves if they should not re-run in a later cycle.  When one exits
  53. # "0", the stack is stopped, so the caller can return to the main rootfs
  54. # wait loop.
  55. try_failure_hooks()
  56. {
  57.     local hook
  58.  
  59.     # Disable usplash so text from hooks can be seen
  60.     if [ -x /sbin/usplash_write ]; then
  61.         /sbin/usplash_write "QUIT"
  62.     fi
  63.     chvt 1
  64.  
  65.     for hook in /tmp/mountroot-fail-hooks.d/*; do
  66.         if [ -x ${hook} ] && ${hook} mountfail; then
  67.             return 0
  68.         fi
  69.     done
  70.     return 1
  71. }
  72.  
  73. panic()
  74. {
  75.     if [ -x /sbin/usplash_write ]; then
  76.         /sbin/usplash_write "QUIT"
  77.     fi
  78.     chvt 1
  79.  
  80.     # Disallow console access
  81.     if [ -n "${panic}" ]; then
  82.         sleep ${panic}
  83.         reboot
  84.     fi
  85.  
  86.     modprobe i8042
  87.     modprobe atkbd
  88.     echo $@
  89.     PS1='(initramfs) ' /bin/sh -i </dev/console >/dev/console 2>&1
  90. }
  91.  
  92. maybe_break()
  93. {
  94.     if [ "${break}" = "$1" ]; then
  95.         panic "Spawning shell within the initramfs"
  96.     fi
  97. }
  98.  
  99. render()
  100. {
  101.     eval "echo -n \${$@}"
  102. }
  103.  
  104. set_initlist()
  105. {
  106.     unset initlist
  107.     for si_x in ${initdir}/*; do
  108.         # skip empty dirs without warning
  109.         [ "${si_x}" = "${initdir}/*" ] && return
  110.  
  111.         # only allow variable name chars
  112.         case ${si_x#${initdir}/} in
  113.         *[![:alnum:]_]*)
  114.             [ "${verbose}" = "y" ] \
  115.             && echo "$si_x ignored: not alphanumeric or '_' file"
  116.             continue
  117.             ;;
  118.         esac
  119.  
  120.         # skip non executable scripts
  121.         if [ ! -x ${si_x} ]; then
  122.             [ "${verbose}" = "y" ] \
  123.             && echo "$si_x ignored: not executable"
  124.             continue
  125.         fi
  126.  
  127.         # skip directories
  128.         if [ -d ${si_x} ]; then
  129.             [ "${verbose}" = "y" ] \
  130.             && echo "$si_x ignored: a directory"
  131.             continue
  132.         fi
  133.  
  134.         initlist="${initlist} ${si_x#${initdir}/}"
  135.     done
  136. }
  137.  
  138. reduce_satisfied()
  139. {
  140.     deplist="$(render array_${1})"
  141.     unset tmpdeplist
  142.     for rs_y in ${deplist}; do
  143.         # check if there are alternatives
  144.         case ${rs_y} in
  145.         *\|*)
  146.             OLD_IFS="$IFS"
  147.             IFS="|"
  148.             for rs_z in ${rs_y}; do
  149.                 IFS="$OLD_IFS"
  150.                 # only allow variable name chars
  151.                 case ${rs_z} in
  152.                 *[![:alnum:]_]*)
  153.                         IFS="|"
  154.                         continue
  155.                         ;;
  156.                 esac
  157.                 # skip non executable scripts
  158.                 if [ ! -x ${initdir}/${rs_z} ]; then
  159.                     IFS="|"
  160.                     continue
  161.                 fi
  162.                 # skip directories
  163.                 if [ -d ${initdir}/${rs_z} ]; then
  164.                     IFS="|"
  165.                     continue
  166.                 fi
  167.                 tmpdeplist="${tmpdeplist} ${rs_z}"
  168.                 break
  169.             done
  170.             IFS="$OLD_IFS"
  171.             ;;
  172.         *)
  173.             case ${rs_y} in
  174.             *[![:alnum:]_]*)
  175.                 continue
  176.                 ;;
  177.             esac
  178.             if [ ! -x ${initdir}/${rs_y} ]; then
  179.                 continue
  180.             fi
  181.             if [ -d ${initdir}/${rs_y} ]; then
  182.                 continue
  183.             fi
  184.             tmpdeplist="${tmpdeplist} ${rs_y}"
  185.             ;;
  186.         esac
  187.     done
  188.     deplist=${tmpdeplist}
  189.     for rs_x in ${runlist}; do
  190.         pop_list_item ${rs_x} ${deplist}
  191.         deplist=${tmppop}
  192.     done
  193.     eval array_${1}=\"${deplist}\"
  194. }
  195.  
  196. get_prereqs()
  197. {
  198.     set_initlist
  199.     for gp_x in ${initlist}; do
  200.         tmp=$(${initdir}/${gp_x} prereqs)
  201.         eval array_${gp_x}=\"${tmp}\"
  202.     done
  203. }
  204.  
  205. count_unsatisfied()
  206. {
  207.     set -- ${@}
  208.     return ${#}
  209. }
  210.  
  211. # Removes $1 from initlist
  212. pop_list_item()
  213. {
  214.     item=${1}
  215.     shift
  216.     set -- ${@}
  217.     unset tmppop
  218.     # Iterate
  219.     for pop in ${@}; do
  220.         if [ ${pop} = ${item} ]; then
  221.             continue
  222.         fi
  223.         tmppop="${tmppop} ${pop}"
  224.     done
  225.  
  226. }
  227.  
  228. # This function generates the runlist, so we clear it first.
  229. reduce_prereqs()
  230. {
  231.     unset runlist
  232.     set -- ${initlist}
  233.     i=$#
  234.     # Loop until there's no more in the queue to loop through
  235.     while [ ${i} -ne 0 ]; do
  236.         oldi=${i}
  237.         for rp_x in ${initlist}; do
  238.             reduce_satisfied ${rp_x}
  239.             count_unsatisfied $(render array_${rp_x})
  240.             cnt=${?}
  241.             if [ ${cnt} -eq 0 ]; then
  242.                 runlist="${runlist} ${rp_x}"
  243.                 pop_list_item ${rp_x} ${initlist}
  244.                 initlist=${tmppop}
  245.                 i=$((${i} - 1))
  246.             fi
  247.         done
  248.         if [ ${i} -eq ${oldi} ]; then
  249.             panic "PANIC: Circular dependancy.  Exiting."
  250.         fi
  251.     done
  252. }
  253.  
  254. call_scripts()
  255. {
  256.     for cs_x in ${runlist}; do
  257.         if [ x"$1" = "xoptional" ]; then
  258.             option=$(sed '/^OPTION=/!d;$d;s/^OPTION=//;s/[[:space:]]*$//' "${initdir}/${cs_x}")
  259.             [ -z "${option}" ] || eval test -n \"\${$option}\" -a \"\${$option}\" != \"n\" || continue
  260.         fi
  261.  
  262.         # mkinitramfs verbose output
  263.         if [ "${verbose}" = "y" ]; then
  264.             echo "Calling hook ${cs_x}"
  265.         fi
  266.         ${initdir}/${cs_x}
  267.         # allow boot scripts to modify exported boot paramaters
  268.         if [ -e /conf/param.conf ]; then
  269.             . /conf/param.conf
  270.         fi
  271.     done
  272. }
  273.  
  274. run_scripts()
  275. {
  276.     initdir=${1}
  277.     [ ! -d ${initdir} ] && return
  278.     get_prereqs
  279.     reduce_prereqs
  280.     call_scripts $2
  281. }
  282.  
  283. # Load custom modules first
  284. load_modules()
  285. {
  286.     if [ -e /conf/modules ]; then
  287.         cat /conf/modules | while read m; do
  288.             # Skip empty lines
  289.             if [ -z "$m" ];  then
  290.                 continue
  291.             fi
  292.             # Skip comments - d?ash removes whitespace prefix
  293.             com=$(printf "%.1s" "${m}")
  294.             if [ "$com" = "#" ]; then
  295.                 continue
  296.             fi
  297.             modprobe $m
  298.         done
  299.     fi
  300. }
  301.  
  302. # lilo compatibility
  303. parse_numeric() {
  304.     case $1 in
  305.     "")
  306.         return
  307.         ;;
  308.     /*)
  309.         return
  310.         ;;
  311.     *:*)
  312.         minor=${1#*:}
  313.         major=${1%:*}
  314.         ;;
  315.     *)
  316.         value=$(( 0x${1} ))
  317.         minor=$(( ${value} % 256 ))
  318.         major=$(( ${value} / 256 ))
  319.         ;;
  320.     esac
  321.  
  322.     mknod -m 600 /dev/root b ${major} ${minor}
  323.     ROOT=/dev/root
  324. }
  325.  
  326. configure_networking()
  327. {
  328.     # networking already configured thus bail out
  329.     [ -n "${DEVICE}" ] && [ -e /tmp/net-"${DEVICE}".conf ] && return 0
  330.  
  331.     # support ip options see linux sources Documentation/nfsroot.txt
  332.     case ${IPOPTS} in
  333.     none|off)
  334.         # Do nothing
  335.         ;;
  336.     ""|on|any)
  337.         # Bring up device
  338.         ipconfig -t 60 ${DEVICE}
  339.         ;;
  340.     dhcp|bootp|rarp|both)
  341.         ipconfig -t 60 -c ${IPOPTS} -d ${DEVICE}
  342.         ;;
  343.     *)
  344.         ipconfig -t 60 -d $IPOPTS
  345.  
  346.         # grab device entry from ip option
  347.         NEW_DEVICE=${IPOPTS#*:*:*:*:*:*}
  348.         if [ "${NEW_DEVICE}" != "${IPOPTS}" ]; then
  349.             NEW_DEVICE=${NEW_DEVICE%:*}
  350.         else
  351.             # wrong parse, possibly only a partial string
  352.             NEW_DEVICE=
  353.         fi
  354.         if [ -n "${NEW_DEVICE}" ]; then
  355.             DEVICE="${NEW_DEVICE}"
  356.         fi
  357.         ;;
  358.     esac
  359.  
  360.     # source ipconfig output
  361.     if [ -n "${DEVICE}" ]; then
  362.         # source specific bootdevice
  363.         . /tmp/net-${DEVICE}.conf
  364.     else
  365.         # source any interface as not exaclty specified
  366.         . /tmp/net-*.conf
  367.     fi
  368. }
  369.